package com.news.controller;

import com.news.config.Config;
import com.news.pojo.UserBean;
import com.news.pojo.Base.BaseListResult;
import com.news.pojo.Base.BaseResult;
import com.news.service.JokeService;
import com.news.service.UserService;
import com.news.utils.IDUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import static com.news.config.Config.SUCCESS_CODE;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：big_news
 * created on  ：2021-06-17
 * description：
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    JokeService jokeService;

    @RequestMapping(value = "/regist",method = RequestMethod.POST)
    @ResponseBody
    public BaseResult regist(UserBean userBean) {
        BaseResult baseResult = new BaseResult();
        List<UserBean> list = new ArrayList<>();
        try {
            UserBean user = new UserBean();
            user.setName(userBean.getName());
            user.setPassword(userBean.getPassword());
            user.setNickname(userBean.getNickname());
            user.setUserId(IDUtils.RandomId());
            //得到当前时间 更新为注册时间 
            user.setRegistTime(new Date());
            //验证当前输入的用户是否已经注册
            UserBean tempBean1 = userService.getUserByName(userBean.getName());
            if (tempBean1 != null) {
                baseResult.setCode(Config.ERROR_CODE);
                baseResult.setMsg("该用户名已注册");
                return baseResult;
            }
          //验证当前输入的昵称是否已经拥有
            UserBean tempBean2 = userService.getUserByNick(userBean.getNickname());
            if (tempBean2 != null) {
                baseResult.setCode(Config.ERROR_CODE);
                baseResult.setMsg("该昵称已存在");
                return baseResult;
            }
           //添加用户数据到数据库
            userService.addUser(user);
            list.add(user);
            baseResult.setData(list);
            baseResult.setCode(Config.SUCCESS_CODE);
            baseResult.setMsg("注册成功");
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public BaseResult login(UserBean userBean) {
        BaseResult baseResult = new BaseResult();
        List<UserBean> list = new ArrayList<>();
        System.out.println(userBean);
        System.out.println(userBean.getPassword());
        try {
        	UserBean userBean1 = userService.getUserByName(userBean.getName());
            if (userBean1 == null) {
                baseResult.setMsg("不存在该用户");//
                baseResult.setCode(Config.ERROR_CODE);//状态码是 -1
                return baseResult;
            } else if (!userBean1.getPassword().equals(userBean.getPassword())) {
                baseResult.setMsg("密码错误");
                baseResult.setCode(Config.ERROR_CODE);
                return baseResult;
            } else {
                list.add(userBean1);//将查询到的数据放到list集合中
                baseResult.setData(list);//将数据封装到base返回到前端
                baseResult.setCode(Config.SUCCESS_CODE);//封装 状态码是 200
                baseResult.setMsg("登录成功");
                //获取当前时间为最后一次登录时间
                userBean.setLastLoginTime(new Date());
                //更新登录时间
                userService.updateUser(userBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setMsg(Config.MES_SERVER_ERROR);//系统出现异常，请稍后再试..
            baseResult.setCode(Config.ERROR_CODE);//-1
        }
        System.out.println(baseResult);
        return baseResult;
    }

    
    //在个人主页 点击 我的 的时候执行这里
    @RequestMapping(value = "/selfJokes")
    @ResponseBody
    public BaseListResult getSelfJokes(@RequestParam(value = "page",required = false) Integer page,
                                       @RequestParam(value = "row",required = false) Integer row,
                                       @RequestParam(value = "userId",required = false) String userId) {
        BaseListResult baseResult = new BaseListResult();
        
        System.out.println(page);//page=1
        System.out.println(row);//row=10
        System.out.println(userId);// y_user表的userId
        
        try {
            BaseListResult result = jokeService.getUserSelfJokes(page, row, userId);
            if (result != null) {
                result.setCode(SUCCESS_CODE);
                result.setMsg(Config.MES_REQUEST_SUCCESS);
                return result;
            }
            baseResult.setCode(Config.ERROR_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;

    }

    @RequestMapping(value = "/thumb")
    @ResponseBody
    public BaseListResult getSelfLikeJokesById(@RequestParam(value = "page",required = false) Integer page,
                                               @RequestParam(value = "row",required = false) Integer row,
                                               @RequestParam(value = "userId",required = false) String userId) {
        BaseListResult baseResult = new BaseListResult();
        try {
            BaseListResult result = jokeService.getJokeLikeByUserId(page, row, userId);
            if (result != null) {
                result.setCode(SUCCESS_CODE);
                result.setMsg(Config.MES_REQUEST_SUCCESS);
                return result;
            }
            baseResult.setCode(Config.ERROR_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;

    }

    @RequestMapping(value = "/userInfo")
    @ResponseBody
    public BaseResult getUserInfo(@RequestParam(value = "userId") String userId) {
        BaseResult baseResult = new BaseResult();
        List<UserBean> list = new ArrayList<>();
        try {
            UserBean userBean = userService.getUserById(userId);
            list.add(userBean);
            baseResult.setData(list);
            baseResult.setCode(SUCCESS_CODE);
            baseResult.setMsg(Config.MES_REQUEST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;
    }

    @RequestMapping(value = "/userInfo/modify")
    @ResponseBody
    public BaseResult modifyUserInfo(@RequestParam(value = "userId",required = false) String userId,
                                     @RequestParam(value = "talk",required = false) String talk,
                                     @RequestParam(value = "address",required = false) String address) {
        BaseResult baseResult = new BaseResult();
        List<UserBean> list = new ArrayList<>();
        try {
            UserBean userBean = userService.getUserById(userId);
            userBean.setTalk(talk);
            userBean.setAddress(address);
            userService.updateUser(userBean);
            list.add(userBean);
            baseResult.setData(list);
            baseResult.setCode(SUCCESS_CODE);
            baseResult.setMsg(Config.MES_REQUEST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;
    }

}
